//
//  FriendCricleCell.swift
//  jiahui
//
//  Created by 3tee on 2024/12/12.
//

import UIKit
import SnapKit

class FriendCricleCell: UITableViewCell {

    let padding = 15
    let magin = 8
    
    var model: FriendCricleModel?{
        didSet{
            iconImgV.image = UIImage(named: model?.icon ?? "male")
            nameLab.text = model?.name
            contentLab.text = model?.content
            print("当前文本高度 = \(model?.contentHeight)")
            contentLab.snp.updateConstraints { make in
                make.height.equalTo(model?.contentHeight ?? 0)
            }
            if model?.contentHeight ?? 0 < 100 {
                openBtn.isHidden = true
                openBtn.snp.updateConstraints { make in
                    make.height.equalTo(0)
                }
            }else{
                openBtn.isHidden = false
                openBtn.isSelected = model?.isOpen ?? false
                openBtn.snp.updateConstraints { make in
                    make.height.equalTo(25)
                }
            }
            
            if model?.medias.count != 0 {
                collectionView.snp.updateConstraints { make in
                    //make.top.equalTo(openBtn.snp.bottom).offset(magin)
                    make.height.equalTo(model?.collectionHeight ?? 0)
                }
            }else{
                collectionView.snp.updateConstraints { make in
                    make.height.equalTo(0)
                }
            }
           
            collectionView.reloadData()
           
        }
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    func setupUI() {
        self.contentView.addSubview(iconImgV)
        iconImgV.snp.makeConstraints { make in
            make.left.equalTo(padding)
            make.top.equalTo(padding)
            make.width.height.equalTo(36)
        }
        
        self.contentView.addSubview(nameLab)
        nameLab.snp.makeConstraints { make in
            make.left.equalTo(iconImgV.snp.right).offset(magin)
            make.top.equalTo(iconImgV.snp.top)
            make.right.equalTo(-padding)
            make.height.equalTo(16)
        }
        
      
        self.contentView.addSubview(contentLab)
        contentLab.snp.makeConstraints { make in
            make.left.equalTo(iconImgV.snp.right).offset(magin)
            make.top.equalTo(nameLab.snp.bottom).offset(magin)
            make.right.equalTo(-padding)
            make.height.equalTo(0)
        }
        
        self.contentView.addSubview(openBtn)
        openBtn.snp.makeConstraints { make in
            make.left.equalTo(contentLab.snp.left)
            make.top.equalTo(contentLab.snp.bottom)
            make.width.equalTo(40)
            make.height.equalTo(25)
        }
        
        
        self.contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.left.equalTo(iconImgV.snp.right).offset(magin)
            make.top.equalTo(openBtn.snp.bottom).offset(magin)
            make.right.equalTo(-padding)
            make.height.equalTo(0)
        }
        

    }
    
    //头像
    lazy var iconImgV: UIImageView = {
        //$0.backgroundColor = .brown
        $0.layer.cornerRadius = 5
        $0.layer.masksToBounds = true
        return $0
    }(UIImageView())
    
    //姓名
    lazy var nameLab: UILabel = {
        //$0.backgroundColor = .brown
        $0.text = "卡皮巴拉"
        $0.textColor = .blue
        $0.font = .systemFont(ofSize: 16)
        $0.textAlignment = .left
        return $0
    }(UILabel())
    
    //内容
    lazy var contentLab: UILabel = {
        $0.backgroundColor = .brown
        $0.textColor = .darkText
        $0.numberOfLines = 0
        $0.text = "测试描述"
        $0.font = .systemFont(ofSize: 16)
        $0.textAlignment = .left
        return $0
    }(UILabel())
    
    //全文
    lazy var openBtn: UIButton = {
        $0.setTitle("全文", for:.normal)
        $0.setTitle("收起", for:.selected)
        $0.setTitleColor(.blue, for: .normal)
        return $0
    }(UIButton(type: .custom))

    //媒体内容
    lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumInteritemSpacing = 8
        let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        collectionView.register(FriendCricleMediaCell.self, forCellWithReuseIdentifier: "mediaCell")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isScrollEnabled = false
        return collectionView
    }()
    
 
    
}

extension FriendCricleCell: UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model?.medias.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mediaCell", for: indexPath) as! FriendCricleMediaCell
        cell.backgroundColor = .brown
        if let imgName = model?.medias[indexPath.row] {
            let img = UIImage(named: imgName)
            cell.imgV.image = img
        }
        cell.gestureHandler = { cell in
            let idx = collectionView.indexPath(for: cell)
            print("当前点击的idx = \(idx?.row)")
            
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       
        return model?.collectionItemSize ?? CGSizeZero
    }
    
}
